//Complex.cpp
#include <iostream>
#include "Complex.h"
using namespace std;

Complex::Complex() {}

Complex::Complex(double re, double im) : real(re), imag(im) {}

Complex::Complex(const Complex& c) {
    this->real = c.real;
    this->imag = imag;
}

Complex::~Complex() {}

Complex operator+(const Complex c1, const Complex c2) {
    Complex c(c1.real + c2.real, c1.imag + c2.imag);
    return c;
}

Complex operator-(const Complex c1, const Complex c2) {
    Complex c(c1.real - c2.real, c1.imag - c2.imag);
    return c;
}

Complex operator*(const Complex c1, const Complex c2) {
    Complex c(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c2.real * c1.imag);
    return c;
}

Complex operator/(const Complex c1, const Complex c2) {
    if ((c2.real * c2.real + c2.imag * c2.imag) == 0) {
        cout << "被除数不能为0" << endl;
        return NULL;
    } else {
        Complex c(c1.real * c2.real + c1.imag * c2.imag, -c1.real * c2.imag + c2.real * c1.imag);
        c.real = c.real / (c2.real * c2.real + c2.imag * c2.imag);
        c.imag = c.imag / (c2.real * c2.real + c2.imag * c2.imag);
        return c;
    }
}

Complex& Complex::operator=(const Complex c) {
    if (this != &c) {
        this->real = c.real;
        this->imag = c.imag;
    }
    return *this;
}

ostream& operator<<(ostream& out, const Complex& c) {
    if (c.real == 0) {
        if (c.imag == 0) { //实部虚部都为0
            //(0)
            out << "(" << 0 << ")";
        } else {    //实部为0虚部不为0
            //(-|""abs(imag)i)
            out << "(" << (c.imag > 0 ? "" : "-") 
            << (c.imag > 0 ? c.imag : -c.imag) << "i)";
        }
    } else {
        if (c.imag == 0) { //实部不为0虚部为0
            //(real)
            out << "(" << c.real << ")";
        } else {//实部虚部都不为0
            out << "(" << c.real << (c.imag > 0 ? "+" : "-") 
            << (c.imag > 0 ? c.imag : -c.imag) << "i)";
        }
    }
    return out;
}